home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-13 | 6.2 KB | 302 lines | [TEXT/ttxt] |
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- class ARRAY[E]
- --
- -- General purpose arrays.
- --
-
- inherit ARRAYED_COLLECTION[E];
-
- creation make, with_capacity, from_collection
-
- feature
-
- lower: INTEGER;
- -- Lower index bound.
-
- feature -- Creation and Modification :
-
- make(minindex, maxindex: INTEGER) is
- -- Make array with range [minindex .. maxindex].
- -- When maxindex = minindex - 1, the array is empty.
- require
- minindex -1 <= maxindex
- local
- needed: INTEGER;
- do
- lower := minindex;
- upper := maxindex;
- needed := maxindex - minindex + 1;
- if needed /= 0 then
- if capacity < needed then
- if capacity = 0 then
- storage.malloc(needed);
- else
- storage.realloc(storage,needed);
- end;
- capacity := needed;
- end;
- clear_all;
- end;
- ensure
- lower = minindex;
- upper = maxindex;
- all_cleared
- end;
-
- with_capacity(needed_capacity, low: INTEGER) is
- -- Create an empty array with `capacity' initialized
- -- at least to `needed_capacity' and `lower' set to `low'.
- require
- needed_capacity >= 0
- do
- if capacity < needed_capacity then
- if capacity = 0 then
- storage.malloc(needed_capacity);
- else
- storage.realloc(storage,needed_capacity);
- end;
- capacity := needed_capacity;
- end;
- lower := low;
- upper := low - 1;
- ensure
- empty;
- needed_capacity <= capacity;
- lower = low
- end;
-
- feature -- Modification :
-
- resize(minindex, maxindex: INTEGER) is
- -- Resize the array. No elements will be lost in the
- -- intersection of [old lower .. old upper] and
- -- [minindex .. maxindex].
- -- New positions are initialized with appropriate
- -- default values.
- require
- minindex -1 <= maxindex
- local
- other: like Current;
- i, up: INTEGER;
- mem: MEMORY;
- do
- from
- !!other.make(minindex,maxindex);
- i := lower.max(other.lower);
- up := upper.min(other.upper);
- until
- i > up
- loop
- other.put(item(i),i);
- i := i + 1;
- end;
- if storage.is_not_void then
- storage.free;
- end;
- standard_copy(other);
- mem.free(other.to_pointer);
- end;
-
- feature
-
- sub_array(low, up: INTEGER): like Current is
- local
- i: INTEGER;
- do
- from
- !!Result.with_capacity(up - low + 1,low);
- Result.set_upper(up);
- i := low;
- until
- i > up
- loop
- Result.put(item(i),i);
- i := i + 1;
- end;
- ensure then
- Result.lower = low;
- end;
-
- feature -- Implementation of deferred :
-
- count: INTEGER is
- do
- Result := upper - lower + 1;
- end;
-
- item, infix "@" (index: INTEGER): E is
- do
- Result := storage.item(index - lower);
- end;
-
- put(element: like item; index: INTEGER) is
- do
- storage.put(element,index - lower);
- end;
-
- force(element: like item; index: INTEGER) is
- -- Put `element' at position `index'. Collection is
- -- resized if `index' is not inside current bounds.
- -- New bounds are initialized with default values.
- require else
- true
- do
- if upper < index then
- resize(lower,index);
- elseif index < lower then
- resize(index,upper);
- end;
- put(element,index);
- end;
-
- copy(other: like Current) is
- -- Copy `other' onto Current.
- local
- needed_capacity: INTEGER;
- do
- lower := other.lower;
- upper := other.upper;
- needed_capacity := upper - lower + 1;
- if capacity < needed_capacity then
- if capacity = 0 then
- capacity := needed_capacity;
- storage.malloc(capacity);
- else
- capacity := needed_capacity;
- storage.realloc(storage,capacity);
- end;
- end;
- if needed_capacity > 0 then
- storage.copy_from(other.storage,needed_capacity - 1);
- end;
- end;
-
- set_all_with(v: like item) is
- do
- storage.set_all_with(v,upper - lower);
- end;
-
- remove_first is
- do
- storage.remove_first(upper - lower);
- lower := lower + 1;
- ensure then
- upper = old upper;
- end;
-
- remove(index: INTEGER) is
- do
- storage.remove(index - lower,upper - lower);
- upper := upper - 1;
- end;
-
- clear is
- do
- upper := lower - 1;
- ensure then
- capacity = old capacity
- end;
-
- add_first(element: like item) is
- do
- if upper < lower then
- add_last(element);
- else
- add_last(element);
- move(lower,upper - 1,1);
- put(element,lower);
- end;
- end;
-
- add_last(element: like item) is
- do
- if capacity < count + 1 then
- if capacity = 0 then
- capacity := 16;
- storage.malloc(capacity);
- else
- capacity := 2 * capacity;
- storage.realloc(storage,capacity);
- end;
- end;
- upper := upper + 1;
- put(element,upper);
- end;
-
- from_collection(model: COLLECTION[like item]) is
- local
- i, up: INTEGER;
- do
- from
- with_capacity(model.count,model.lower);
- i := model.lower;
- up := model.upper;
- upper := up;
- until
- i > up
- loop
- put(model.item(i),i);
- i := i + 1;
- end;
- ensure then
- lower = model.lower;
- upper = model.upper
- end;
-
- all_cleared: BOOLEAN is
- do
- Result := storage.all_cleared(upper - lower);
- end;
-
- nb_occurrences(element: like item): INTEGER is
- do
- Result := storage.nb_occurrences(element,upper - lower);
- end;
-
- fast_nb_occurrences(element: like item): INTEGER is
- do
- Result := storage.fast_nb_occurrences(element,upper - lower);
- end;
-
- index_of(element: like item): INTEGER is
- do
- Result := lower + storage.index_of(element,upper - lower);
- end;
-
- fast_index_of(element: like item): INTEGER is
- do
- Result := lower + storage.fast_index_of(element,upper - lower);
- end;
-
- is_equal(other: like Current): BOOLEAN is
- do
- if Current = other then
- Result := true;
- elseif lower = other.lower and then upper = other.upper then
- if count = 0 then
- Result := true;
- else
- Result := storage.memcmp(other.storage,count);
- end;
- end;
- end;
-
- slice(low, up: INTEGER): like Current is
- local
- i: INTEGER;
- do
- from
- i := low;
- !!Result.with_capacity(up - low + 1,lower);
- until
- i > up
- loop
- Result.add_last(item(i));
- i := i + 1;
- end;
- end;
-
- end -- ARRAY[E]
-